/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "statusbar.h"

#include "ftpushbutton.h"
#include "ftlabel.h"
#include "fthelpwidget.h"
#include "layertrack.h"
#include "mainselector.h"

#include "freetrack.h"
#include "layermanager.h"
#include "trackgps.h"

extern LayerManager* layerManager;
extern FreeTrack* freetrackObject;
extern TrackGps* gps;
extern MainSelector* mainSelector;

StatusBar::StatusBar(QWidget *parent) :
    FreeTrackWidget(parent)
{
    // The menu tool button:
    menu = new FTPushButton(QIcon(":/icons/open_menu.png"), QSize( 96,48 ));
    menu->setCheckable( true );
    menu->setChecked( true );
    menu->setBackground( QBrush( QColor(), Qt::NoBrush ));
    connect( menu, SIGNAL(clicked(bool)),
             freetrackObject, SLOT(showSelector(bool)) );

    // The status GPS status label
    gpsStatus = new FTLabel( tr("GPS") );
    gpsStatus->setAlignment( Qt::AlignCenter );

    // The LiveTrack tracking status label
    trackingStatus = new FTPushButton( tr("Live!") );
    trackingStatus->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::MinimumExpanding );
    trackingStatus->setForeground( QPen( QColor("white") ) );

    // The LiveStatus detailed status label
    liveStatus = new FTLabel();
    liveStatus->setAlignment( Qt::AlignCenter );

    statusHelp = new FTHelpWidget( tr(
            "<center|b>Status Bar</center>"\
            "<justify>This is the FreeTrack status bar. You can see: "\
            "the 'menu' button, the 'gps status' button, the 'live status' button "\
            "and the LiveTrack info label.</justify>"\
            "<justify>The 'menu' button will open the switch page you can use to access "\
            "all the functions of FreeTrack.</justify>"\
            "<justify>The 'gps status' button will change color: green when the GPS has a fix, "\
            "and red when the gps does not have a fix.</justify>"\
            "<justify>The 'live status' button will let you start or stop recording a Live track "\
            "by clicking on it. Also, it is green when a LiveTrack is being recorded and red "\
            "when a live track is not being recorded.</justify>"\
            "<justify>The LiveTrack info label reports the lenght both in Km and points of "\
            "the current Live Track.</justify>"));

    // setup initial status icons
    updateTrackingStatus(false);
    updateGpsStatus(false);
    updateLiveStatus(0,0.0);

    // Connect signals to the status panel (updates)
    connect(layerManager->getLivetrack(), SIGNAL( trackChanged(int,qreal) ),
            this, SLOT(updateLiveStatus(int,qreal)) );

    // Connect signals to the status panel (updates)
    connect(  gps, SIGNAL(gpsStatusChange(bool)),
             this, SLOT(updateGpsStatus(bool)));
    connect(  mainSelector, SIGNAL(liveStatusChange(bool)),
             this, SLOT(updateTrackingStatus(bool)));
    connect( trackingStatus, SIGNAL(clicked()),
               mainSelector, SLOT(changeTracking()));

}

QLayout* StatusBar::createHorizontal()
{
    QHBoxLayout* status_grid = new QHBoxLayout();
    status_grid->addWidget( menu );
    status_grid->addWidget( gpsStatus );
    status_grid->addWidget( trackingStatus );
    status_grid->addWidget( liveStatus, 2 );
    status_grid->addWidget( statusHelp );
    return status_grid;
}

void StatusBar::uncheck()
{
    menu->setChecked(false);
}

QLayout* StatusBar::createVertical()
{
    QHBoxLayout* grid = new QHBoxLayout();
    QVBoxLayout* left = new QVBoxLayout();
    left->addWidget( liveStatus );
    QHBoxLayout* status_grid = new QHBoxLayout();
    status_grid->addWidget( menu );
    status_grid->addWidget( gpsStatus );
    status_grid->addWidget( trackingStatus );
    left->addLayout( status_grid );
    grid->addLayout( left );
    grid->addWidget( statusHelp );
    return grid;
}


void StatusBar::updateGpsStatus(bool timeout)
{
    gpsStatus->setBackground( timeout ?
                              QBrush(QColor("green"), Qt::SolidPattern) :
                              QBrush( QColor("red"), Qt::SolidPattern) );
}

void StatusBar::updateTrackingStatus(bool trklv)
{
    trackingStatus->setBackground( trklv ?
                                   QBrush(QColor("green"), Qt::SolidPattern) :
                                   QBrush( QColor("red"), Qt::SolidPattern) );
}

void StatusBar::updateLiveStatus(int points, qreal kms)
{
    QString str = (kms <=0) ? QString("--") : QString("%1Km").arg(kms, 0, 'f', 2);
    str += (points <= 0) ? QString("/--") : QString("/%1").arg(points);
    liveStatus->setText( str );
}
